package xyz.yq56.easytool.utils.io;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;
import xyz.yq56.easytool.utils.http.HttpsUtil;
import xyz.yq56.easytool.utils.nvll.NullUtil;

/**
 * @author yi qiang
 * @date 2021/9/17 2:30
 */
@Slf4j
public class FileUtil {

    private FileUtil() {
    }

    public static List<String> readByLine(String filePath) {
        List<String> result = null;
        try (InputStream inputStream = new FileInputStream(new File(filePath));
             BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        ) {
            result = bufferedReader.lines().collect(Collectors.toList());
        } catch (Exception e) {
            log.error("文件读取失败,{}", e.getMessage(), e);
        }
        return NullUtil.emptyIfNull(result);
    }


    /**
     * 处理https GET/POST请求 请求地址、请求方法、参数
     */
    public static String httpsRequest(String requestUrl, String msg) {
        try {
            URLConnection conn = HttpsUtil.urlConnection(requestUrl);
            conn.connect();
            // 往服务器端的参数
            if (null != msg) {
                OutputStream os = conn.getOutputStream();
                os.write(msg.getBytes(StandardCharsets.UTF_8));
                os.close();
            }
            // 读取服务器端返回的内容
            InputStream is = conn.getInputStream();
            //读取内容
            InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
            BufferedReader br = new BufferedReader(isr);
            StringBuilder builder = new StringBuilder();
            String line;
            while ((line = br.readLine()) != null) {
                builder.append(line);
            }
            return builder.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 使用示例
     * downLoad("http://i3.sinaimg.cn/blog/2014/1029/S129809T1414550868715.jpg", "NBA.jpg", "C:\\Users\\22477\\Desktop")
     *
     * @param urlStr   urlStr
     * @param fileName 文件名
     * @param savePath 保存路径
     * @throws IOException 异常
     */
    public static void downLoad(String urlStr, String fileName,
                                String savePath) throws IOException {
        InputStream inputStream = HttpsUtil.getInputStream(urlStr);
        if (inputStream != null) {
            byte[] getData = IoUtil.readInputStream(inputStream);
            // 文件保存位置
            File saveDir = new File(savePath);
            if (!saveDir.exists()) {
                saveDir.mkdirs();
            }
            //输出流
            File file = new File(saveDir + File.separator + fileName);
            try (FileOutputStream fos = new FileOutputStream(file)) {
                fos.write(getData);
            }
            inputStream.close();
        }
    }

}
